package cc.rengu.igas.cops.core.service.trans.unionpay;

import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.cops.facade.bean.CouponInfo;
import cc.rengu.igas.cops.facade.request.BankC2bQrCodeQueryRequest;
import cc.rengu.igas.cops.facade.response.BankC2bQrCodeQueryResponse;
import cc.rengu.igas.share.common.enums.UserPayAuthTypeEnum;
import cc.rengu.igas.share.core.realize.UserService;
import cc.rengu.igas.share.core.realize.impl.UserServiceImpl;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.LimitTypeEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSON;

import java.util.List;

/**
 * 被扫---二维码状态查询
 * 手机银行【ESB】—>支付系统
 * <p>
 * BizStatus：
 * 01-业务处理中
 * 40-授权未完成【待密码认证】
 * 41-授权已完成【密码验证完成】
 *
 * @author panky
 * @version 1.0.0 创建时间：2020.06.18
 */
public class BankC2bQrCodeQueryService extends BizBaseService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        BankC2bQrCodeQueryRequest bankC2bQrCodeQueryRequest = new BankC2bQrCodeQueryRequest();
        ConvertUtil.convertOutput(bankC2bQrCodeQueryRequest);
        return bankC2bQrCodeQueryRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        // 录入参数
        BankC2bQrCodeQueryRequest bankC2bAddDealNoticeRequest = (BankC2bQrCodeQueryRequest) request;
        // 行号
        String instId = bankC2bAddDealNoticeRequest.getHeader().getInstId();

        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByOrderUrl(instId, bankC2bAddDealNoticeRequest.getQrNo());

        if (null == userOrderInfo) {
            rglog.debug("查询用户订单表异常");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }
        // 验证方式
        //01 密码验证
        //02 密码+短信验证
        //03 密码+软Ukey
        BankC2bQrCodeQueryResponse bankC2bQrCodeQueryResponse = new BankC2bQrCodeQueryResponse();
        // 商户名称
        bankC2bQrCodeQueryResponse.setMerName(userOrderInfo.getGroupMchntName());// 银联通知的【商户号/门店号】
        // 金额
        if (!StringUtil.isNullorEmpty(userOrderInfo.getRealPayAmt())) {
            bankC2bQrCodeQueryResponse.setTxnAmt(AmountUtil.changeY2F(userOrderInfo.getRealPayAmt()));// 真实支付金额
        } else {
            if (!StringUtil.isNullorEmpty(userOrderInfo.getOrderAmt())) {
                bankC2bQrCodeQueryResponse.setTxnAmt(AmountUtil.changeY2F(userOrderInfo.getOrderAmt()));// 真实支付金额
            }
        }
        // 交易状态
        String bizStt = userOrderInfo.getBizStatus();
        bankC2bQrCodeQueryResponse.setTransState(changeBizStt(bizStt));

        // 请求方自定义域 -- 没有存储
//        bankC2bQrCodeQueryResponse.setReqReserved(qrCodeInfo.getRemark3());
        // 优惠信息类型 TODO
//        bankC2bQrCodeQueryResponse.setType("");
        //优惠信息
        if (!StringUtil.isNullorEmpty(userOrderInfo.getDiscountInfo())) {
            //当有优惠券时，需要返回原交易金额
            List<CouponInfo> list = JSON.parseArray(userOrderInfo.getDiscountInfo(), CouponInfo.class);
            if (list != null && !list.isEmpty()) {
                bankC2bQrCodeQueryResponse.setOrigTxnAmt(AmountUtil.changeY2F(userOrderInfo.getOrderAmt()));// 原交易金额
                if (!StringUtil.isNullorEmpty(userOrderInfo.getRealPayAmt())) {
                    bankC2bQrCodeQueryResponse.setTxnAmt(AmountUtil.changeY2F(userOrderInfo.getRealPayAmt()));// 真实支付金额
                }
                //TODO 优惠券类型
//                bankC2bQrCodeQueryResponse.setType(list.get(0).getType());
            }
        }

        // 商户订单号
        bankC2bQrCodeQueryResponse.setOrderNo(userOrderInfo.getMchntOrderId());
        // 预授权类型
        bankC2bQrCodeQueryResponse.setAuthType(remark1ToAuthType(userOrderInfo.getRemark2()));
        // 商户名
        bankC2bQrCodeQueryResponse.setMerName(userOrderInfo.getGroupMchntName());
        // 初始状态不做判断
        if (!"01".equals(bizStt)) {
            String limitInst = instId + "|" + instId + "|" + userOrderInfo.getUserId();
            UserService userService = new UserServiceImpl();
            // 验证方式
            // 01 密码验证
            //02 密码+短信验证
            //03 密码+软Ukey
            UserPayAuthTypeEnum userPayAuthTypeEnum = userService.getUserPayAuthType(instId, userOrderInfo.getUserId(), userOrderInfo.getOrderAmt(), LimitTypeEnum.CASHIER.getLimitType());
            String authType = userPayAuthTypeEnum.getType();
            bankC2bQrCodeQueryResponse.setCheckType(authType);
        }

        // 返回
        BizResponse bizResponse = new BizResponse();
        bizResponse.setResult(bankC2bQrCodeQueryResponse);// 返回结果
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    private String changeBizStt(String bizStt) {
        String transStt = "";
        switch (bizStt) {
            //  00-业务处理成功 >> 04-成功
            case "00":
                transStt = "04";
                break;
            // 01-业务处理中 >> 02-支付中
            case "01":
                transStt = "00";
                break;
            // 02-业务处理失败 >> 03-失败
            case "02":
                transStt = "03";
                break;
            // 10-已撤销 >> 12-已撤销
            case "10":
                transStt = "12";
                break;
            // 11-已部分撤销 >> 12-已撤销
            case "11":
                transStt = "12";
                break;
            // 20-已冲正 >> 13-已冲正
            case "20":
                transStt = "13";
                break;
            // 21-已部分冲正 >> 13-已冲正
            case "21":
                transStt = "13";
                break;
            // 30-已全额退货 >> 23-已退款
            case "30":
                transStt = "23";
                break;
            // 31-已部分退货 >> 23-已退款
            case "31":
                transStt = "23";
                break;
            // 40-授权未完成 >> 05-待验密
            case "40":
                transStt = "05";
                break;
            // 41-授权已完成 >> 06-验密完成
            case "41":
                transStt = "06";
                break;
            // 50-已确认收货 >> 04-成功
            case "50":
                transStt = "04";
                break;
            // 60-已退汇 >> 23-已退款
            case "60":
                transStt = "23";
                break;
        }

        return transStt;
    }

    // 消费交易0310000903、
    //消费冲正交易0320000903、
    //消费撤销交易0330000903、
    //退货交易0340000903
    //预授权交易0290000903
    //预授权冲正交易0290100903
    //预授权撤销交易0370000903
    //预授权完成交易0380000903
    //预授权完成冲正交易0380100903
    //预授权完成撤销交易0389000903
    private String remark1ToAuthType(String remark1) {
        String authType = "";
        if (remark1 != null)
            switch (remark1) {
                //  消费交易0310000903 >> 空
                case "0310000903":
                    authType = "";
                    break;
                // 预授权交易0290000903 >> 10
                case "0290000903":
                    authType = "10";
                    break;
                // 预授权完成交易0380000903 >> 11
                case "0380000903":
                    authType = "11";
                    break;
                // 消费撤销交易0330000903 >> 12
                case "0330000903":
                    authType = "12";
                    break;
                // 预授权完成撤销交易0389000903 >> 13
                case "0389000903":
                    authType = "13";
                    break;
                // 预授权冲正交易0290100903 >> 14
                case "0290100903":
                    authType = "14";
                    break;
                // 预授权完成冲正交易0380100903 >> 15
                case "0380100903":
                    authType = "15";
                    break;
            }
        return authType;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        BankC2bQrCodeQueryResponse retResponse = (BankC2bQrCodeQueryResponse) response.getResult();
        ConvertUtil.convertInput(retResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
